package com.mz.mattress.client.api.biz;

import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Member;
import com.mz.mattress.api.entity.OrderUseTimes;
import com.mz.mattress.api.jwt.ClientJwt;
import com.mz.mattress.client.api.req.MemberReq;
import com.mz.mattress.client.common.Holder;
import com.mz.mattress.client.service.MemberService;
import com.mz.mattress.client.service.OrderUseTimesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderUseTimesService orderUseTimesService;

    @GetMapping("/getInfo")
    public Result<Member> getInfo() {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Long memberId = clientJwt.getMemberId();
        return memberService.getById(memberId);
    }

    @PostMapping(value = "/save")
    public Result save(@RequestBody MemberReq req) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        req.setMemberId(clientJwt.getMemberId());
        return memberService.updateById(req);
    }

    @RequestMapping(value = "/saveDeviceInfo")
    public void saveDeviceInfo(@RequestParam String deviceCode) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        memberService.saveDeviceInfo(clientJwt.getMemberId(), deviceCode);
    }

    @GetMapping("/reportList")
    public Result<List<OrderUseTimes>> reportList() {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Long memberId = clientJwt.getMemberId();
        return orderUseTimesService.reportList(memberId);
    }

    @GetMapping("/reportDetail")
    public Result<OrderUseTimes> reportDetail(@RequestParam Integer id) {
        return orderUseTimesService.reportDetail(id);
    }

}
